<?php

namespace app\common\dao\approval;

use app\common\dao\BaseDao;
use app\common\model\approval\ApprovalLogModel;

class ApprovalLogDao extends BaseDao
{

    /**
     * @return ApprovalLogModel
     */
    protected function getModel(): string
    {
        return ApprovalLogModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {

        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['flow_id']) && $where['flow_id'] !== '', function ($query) use ($where) {
                $query->where('flow_id', intval($where['flow_id']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     *  根据流程  ID 删除日志
     * @param int $flowId
     * @return bool
     *
     * @author Amos
     * 2024-05-23 10:06
     */
    public function deleteByFlowId(int $flowId)
    {
        return $this->getSearch([
            'flow_id' => $flowId
        ])->delete();
    }
}